home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- * *
- * Module: unix main.c *
- * Programmer: Steve Adams *
- * *
- * (C) Copyright 1985, THINK Technologies, Inc. All rights reserved. *
- * *
- * Alternate main progrUNIX Diff.πe Unix command lines under Lightspeed *
- * C. The user is prompted for the command line on program entry. The *
- * line is disected and placed in an "argv" array up to a maximum of *
- * MAX_ARGS entries. I/O redirection is also supported, as follows: *
- * *
- * > redirects stdout to following file name *
- * < redirects stdin to following file name *
- * >> redirects stderr to following file name *
- * *
- * File names following a redirection may be immediately after the *
- * redirector, or at what appears to be the next argument. If a file *
- * open error occurs, then the program calls "SysBeep" and exits to the *
- * shell. *
- * *
- * TO USE: change the "main" function in you application to "_main" and *
- * include this file in your project. *
- * *
- *************************************************************************/
-
- #include <MacHeaders>
- #include <AppleEvents.h>
- #include <stdarg.h>
-
- #include <stdio.h>
- #include <ctype.h>
- #include "diff.h"
-
- int done;
-
- #define MAX_ARGS 50
-
- #ifndef true
- #define true 1
- #define false 0
- #endif
-
- #ifdef COMMENT
- static int argc = 1; /* final argument count */
- static char *argv[MAX_ARGS] = { "" }; /* array of pointers */
- static char command[256]; /* input line buffer */
- static int filename = false; /* TRUE iff file name */
- #endif
-
- int putByte(int len, char *str);
- int resAlloced, resUsed;
- Handle result_handle;
-
- /*************************************************************************
- * *
- * Local routine to make a "beep" and exit to the shell. *
- * *
- *************************************************************************/
-
- static void punt()
- {
- SysBeep( 5L );
- exit(1);
- }
-
-
- /*************************************************************************
- * *
- * Local routine to open a file in argv[--argc] after closing it's *
- * previous existance. *
- * *
- *************************************************************************/
-
-
- static void openfile( file, mode, argc, argv, filename )
- char *mode; /* mode for file open */
- FILE *file; /* file pointer to use */
- int *argc;
- char **argv;
- int *filename;
- {
-
- if ( (file = freopen( argv[--*argc], mode, file ) ) <= (FILE *) NULL)
- punt();
- *filename = false;
- }
-
-
- /*************************************************************************
- * *
- * New main routine. Prompts for command line then calls user's main *
- * now called "_main" with the argument list and redirected I/O. *
- * *
- *************************************************************************/
-
- #undef DEBUG
-
- #ifdef DEBUG
- char *command = "\"Internal:Desktop Folder:bdir\" \"Internal:Development:Alpha:Tcl:SystemCode\"";
- #endif
-
- static void
- #ifdef DEBUG
- domain (char *oldcommand, int mainFunc(int argc, char *argv[]))
- #else
- domain (char *command, int mainFunc(int argc, char *argv[]))
- #endif
- {
- char c; /* temp for EOLN check */
- register char *cp; /* index in command line */
- char *mode; /* local file mode */
- FILE *file; /* file to change */
- int i;
- int argc = 1; /* final argument count */
- char *argv[MAX_ARGS] = { "" }; /* array of pointers */
- int filename = false; /* TRUE iff file name */
-
-
- cp = &command[0]; /* start of buffer */
- argv[0] = ""; /* program name is NULL */
- while (argc < MAX_ARGS)
- { /* up to MAX_ARGS entries */
- while (isspace( *cp++ ));
- if ( !*--cp )
- break;
- else if ( *cp == '<' )
- { /* redirect stdin */
- cp++;
- file = stdin;
- mode = "r";
- filename = true;
- }
- else if ( *cp == '>' )
- {
- mode = "w";
- filename = true;
- if (*++cp == '>')
- {
- file = stderr;
- cp++;
- }
- else
- file = stdout;
- }
- else
- { /* either an argument or a filename */
- argv[argc++] = cp;
-
- if (*cp == '\"') {
- argv[argc - 1]++;
- while (*++cp && (*cp != '\"'));
- }
- else {
- while ( *++cp && !isspace( *cp ) );
- }
-
- c = *cp;
- *cp++ = '\0';
- if (filename)
- openfile( file, mode, &argc, argv, &filename );
- if (!c)
- break;
- }
- }
- (*mainFunc) ( argc, argv );
- }
-
- pascal OSErr AEDoScriptHandler(AppleEvent *message,AppleEvent *reply,long refnum);
-
- pascal OSErr
- AEDoScriptHandler(AppleEvent *message,AppleEvent *reply,long refnum)
- {
- AEDesc theDesc;
- long length;
- int myerr;
-
- result_handle = NewHandle(0);
- if (result_handle != NULL)
- {
- myerr = AEGetParamDesc(message, keyDirectObject, typeWildCard, & theDesc);
- if (myerr != noErr) return 1;
-
- if (theDesc.descriptorType != (DescType)'TEXT') return 2;
-
- length = GetHandleSize(theDesc.dataHandle);
- SetHandleSize(theDesc.dataHandle, length + 1);
- if (MemError() != noErr) return 3;
- HLock(theDesc.dataHandle);
- * (*theDesc.dataHandle + length) = '\0';
-
- domain (*theDesc.dataHandle, main_diff2);
-
- HUnlock(theDesc.dataHandle);
-
- HLock(result_handle);
- myerr = AEPutParamPtr( reply, keyDirectObject, typeChar, *result_handle, resUsed );
- HUnlock(result_handle);
- DisposHandle(result_handle);
- }
- done = 1;
- return noErr;
- }
-
- int putByte(int len, char *str)
- {
-
- // return;
-
- if ((resAlloced - resUsed) <= len) {
- if (len > resAlloced) resAlloced += 2 * len;
- else (resAlloced *= 2);
-
- SetHandleSize(result_handle, resAlloced);
- if (MemError() != noErr) return 4;
- }
-
- HLock(result_handle);
- strncpy(*result_handle + resUsed, str, len);
- resUsed += len;
- HUnlock(result_handle);
- }
-
-
- int pout(const char *ctrl,...)
- {
- va_list ap;
- char str[256];
-
- va_start(ap,ctrl);
- vsprintf(str,ctrl,ap);
- va_end(ap);
- putByte(strlen(str), str);
- }
-
- static char outstr[4000];
-
- int fpout(FILE *fp, const char *ctrl,...)
- {
- va_list ap;
- int len;
-
- va_start(ap,ctrl);
- vsprintf(outstr,ctrl,ap);
- va_end(ap);
- if ((len = strlen(outstr)) > 1000)
- putByte(10, "<TOO MUCH>");
- else
- putByte(len, outstr);
- }
-
- int petec(int c, FILE *fp)
- {
- char d = c;
- putByte(1, &d);
- }
-
- int petc(int c)
- {
- char d = c;
- putByte(1, &d);
- }
-
- pascal OSErr aeQuit(AppleEvent *, AppleEvent *, long);
-
-
- pascal OSErr
- aeQuit(AppleEvent *event, AppleEvent *reply, long ref)
- {
- exit(1);
- }
-
- void
- main(int argc,char * *argv)
- {
- char command[256]; /* input line buffer */
- EventRecord event;
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitCursor();
-
- #ifdef DEBUG
- InitWindows();
- InitMenus();
- #endif
-
- TEInit();
- InitDialogs(0L);
- SetApplLimit(((void *) (((long) GetApplLimit()) - 32 * 1024)));
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- if (AEInstallEventHandler('misc', 'dosc', AEDoScriptHandler, 0, FALSE) ||
- AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- aeQuit, 0, FALSE)) exit(1);
-
- #ifdef DEBUG
- result_handle = NewHandle(0);
- domain("", main_diff2);
-
- #else
-
- while (1) {
- if (WaitNextEvent(everyEvent,&event, 30, NULL) &&
- (event.what == kHighLevelEvent)) {
- AEProcessAppleEvent(&event);
- if (done) exit(1);
- }
- }
- #endif
- exit(1);
- }
-
-
- size_t pwrite(const void *str, size_t sz, size_t num, FILE *fp)
- {
- putByte(sz * num, (char *)str);
- }
-